using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using SolanumSolstice.Units;
using SolanumSolstice.Mapping;

namespace SolanumSolstice.Buildings
{
    public class SupplyBuilding : Building
    {

        protected bool hasWood;

        protected bool hasFood;

        protected List<Civilian> gatherers;

#region Accessors
        public bool HasWood
        {
            get
            {
            return hasWood;
            }
            set
            {
                hasWood = value;
            }
        }
        public bool HasFood
        {
            get
            {
                return hasFood;
            }
            set
            {
                hasFood = value;
            }
        }
#endregion

        public SupplyBuilding(ContentManager content, string name, Vector2 position, Vector2 entrance, int zombieCount, bool hasWood, bool hasFood)
            : base(content, name, position, entrance, zombieCount)
        {
            this.hasWood = hasWood;
            this.hasFood = hasFood;
            gatherers = new List<Civilian>();

            buildingType = BuildingType.Supply;

            if (hasWood)
            {
                texture = content.Load<Texture2D>("Content\\Txtr\\Bldg\\building_supply_wood");
                width = texture.Width;
                height = texture.Height;
            }
            if (hasFood)
            {
                texture = content.Load<Texture2D>("Content\\Txtr\\Bldg\\building_supply_food");
                width = texture.Width;
                height = texture.Height;
            }

        }

        public override void Update(GameTime gameTime, GamePadState gamePad, GameCursor gameCursor)
        {
            base.Update(gameTime, gamePad, gameCursor);

            
            if (!isInfested)
            {
                List<Civilian> removeList = new List<Civilian>();
                
                foreach (Civilian gatherer in gatherers)
                {
                    if (gatherer.ActionReady)
                    {
                        
                        if (gatherer.IsGathering == true)
                        {
                            if (hasFood == true)
                            {
                                gatherer.GatherFood();
                                gatherer.IsGathering = false;
                            }

                            if (hasWood == true)
                            {
                                gatherer.GatherWood();
                                gatherer.IsGathering = false;

                            }

                            gatherer.RecoveryTime = gatherer.GatherTime;
                            gatherer.InitialRecoveryTime = gatherer.GatherTime;

                            gatherer.ActionReady = false;
                        }
                        else
                        {
                            removeList.Add(gatherer);
                        }
                    }


                }

                foreach (Civilian civ in removeList)
                {
                    ExitBuilding(civ);
                }
            }
        }

        public override void EnterBuilding(Human human)
        {
            base.EnterBuilding(human);

            if (human.UnitClass == UnitClass.Civilian)
            {
                gatherers.Add((Civilian)human);
                human.InBuilding = true;
            }
        }

        public override void ExitBuilding(Unit human)
        {
            base.ExitBuilding(human);

            if (human.UnitClass == UnitClass.Civilian)
            {
                Civilian civ = (Civilian)human;

                gatherers.Remove(civ);

                Building baseDepot = SolanumSolstice.GetBase();

                civ.InBuilding = false;

                civ.SetDepositTarget(baseDepot.Entrance, baseDepot);
            }
        }
    }
}

